#include "init.h"
void init(char *title)
{
    if (SDL_Init(SDL_INIT_VIDEO ) < 0)
    {
        printf("Could not initialize SDL: %s\n", SDL_GetError());
        exit(1);
    }

    jeu.screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0,SDL_HWPALETTE|SDL_DOUBLEBUF);

    if (jeu.screen == NULL)
        {
            printf("Couldn't set screen mode to %d x %d: %s\n", SCREEN_WIDTH, SCREEN_HEIGHT, SDL_GetError
());
            exit(1);
        }

    SDL_WM_SetCaption(title, NULL);

    SDL_ShowCursor(SDL_DISABLE);
   

    if (TTF_Init() < 0)
    {
        printf("Couldn't initialize SDL TTF: %s\n", SDL_GetError());

        exit(1);
    }


    font = loadFont("Shut'Em Down.ttf", 50);
     /* Initialise SDL_Mixer */

   // int flags=MIX_INIT_MP3;
   //int initted=Mix_Init(flags);
  //if( (initted & flags) != flags)
    //{
       // printf("Mix_Init: Failed to init SDL_Mixer\n");
       // printf("Mix_Init: %s\n", Mix_GetError());
      // exit(1);
    //}

      if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024)==-1) {
        printf("Mix_OpenAudio: %s\n", Mix_GetError());
        exit(1);
    }

    Mix_AllocateChannels(32);
}

void loadGame(void)
{
    map.background = loadImage("map.jpg");
    map.startX = 0;
    map.startY = 0;
    map.maxX = map.background->w;
    map.maxY = map.background->h;
    jeu.vies = 3;
    jeu.etoiles = 0;

     jeu.HUD_vie = loadImage("life.png");
     jeu.HUD_etoiles = IMG_Load("wep.png");
     jeu.Shuriken_image = loadImage("shuriken.png");
    /* int colorkey;
     colorkey = SDL_MapRGB(jeu.screen->format,0, 0,0);
 
 
     SDL_SetColorKey(jeu.HUD_etoiles ,SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);*/
    loadSong("greycubes-serialkiller.mp3");
    loadSound();
    jeu.onMenu = 1;
    jeu.menuType = START;

}



void cleanup()
{
     int i;
   
    if (map.background != NULL)
    {
        SDL_FreeSurface(map.background);
    }

    closeFont(font);

    TTF_Quit();
     if (jeu.HUD_vie != NULL)
    {
        SDL_FreeSurface(jeu.HUD_vie);
    }
    if (player.sprite != NULL)
    {
        SDL_FreeSurface(player.sprite);
    }
    for(i = 0 ; i < MONSTRES_MAX ; i++)
    {
        if (monster[i].sprite != NULL)
        {
        SDL_FreeSurface(monster[i].sprite);
        }
    }
    if (jeu.HUD_etoiles != NULL)
    {
        SDL_FreeSurface(jeu.HUD_etoiles);
    }
     freeSound();
    if ( jeu.musique != NULL )
        Mix_FreeMusic(jeu.musique);
    Mix_CloseAudio();
    Mix_Quit();
     if (jeu.Shuriken_image != NULL)
    {
        SDL_FreeSurface(jeu.Shuriken_image);
    }
 

    SDL_Quit();
}
